<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .error-msg {
            display: none;
            color: red;
            font-size: 12px;
        }
    </style>
</head>

<body>
    邮箱: <input type="text">
    <span class="error-msg">请输入正确的邮箱</span>

    <script src="../jquery/jquery.js"></script>
    <script>
        // const emailReg = /^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$/;
        const emailReg = /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;
        $('input').on('blur', function () {
            const v = $(this).val();
            if (!emailReg.test(v)) {
                $('.error-msg').show();
            } else {
                $('.error-msg').hide();
            }
        });

        $('input').on('input', function () {
            const v = $(this).val();
            if (emailReg.test(v)) {
                $('.error-msg').hide();
            }
        });
    </script>
</body>

</html>
